<template>
  <Panel title="账号添加">
    <!-- 主要内容 -->
    <div slot="container">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        size="small"
      >
        <!-- 账号 -->
        <el-form-item label="账号" prop="account">
          <el-input
            type="text"
            v-model="ruleForm.account"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- 用户组 -->
        <el-form-item label="用户组" prop="userGroup">
          <el-select v-model="ruleForm.userGroup" placeholder="请选择用户组">
            <el-option label="普通管理员" value="普通管理员"></el-option>
            <el-option label="超级管理员" value="超级管理员"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" size="small" @click="addAcc"
            >添加</el-button
          >
          <el-button size="small" @click="resetForm('ruleForm')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </Panel>
</template>

<script>
import Panel from "@/components/Panel.vue";

//引入验证规则
import { checkAcc, checkPass } from "@/utils/validate.js";

//引入AJAX
import { reqAddAcc, reqAccRepeat } from "@/api/user.js";

export default {
  components: {
    Panel,
  },

  data() {
    //验证账号
    var validateAcc = (rule, value, callback) => {
      //1.value不能为空
      //2.value必需通过正则
      //3.校验通过
      if (value == "") {
        callback(new Error("账号不能为空"));
      } else if (!checkAcc(value)) {
        callback(
          new Error(
            "账号不符合规则:中文、英文、数字但不包括下划线等符号,长度2到12"
          )
        );
      } else {
        //验证是否重名
        reqAccRepeat({
          account: value,
        }).then((res) => {
          //解构
          console.log(res);
          let { code, msg } = res.data;
          //判定
          if (code == 200) {
            //账号后台没有,可以使用
            callback();
          } else {
            //后台有当前账号,不可以注册使用
            callback(new Error(msg));
          }
        });
      }
    };

    //验证密码
    var validatePass = (rule, value, callback) => {
      //1.value不能为空
      //2.value必需通过正则
      //3.校验通过
      if (value == "") {
        callback(new Error("密码不能为空"));
      } else if (!checkPass(value)) {
        callback(
          new Error(
            "密码不符合规则：以字母开头,长度在6~18之间,包含字母、数字和下划线"
          )
        );
      } else {
        callback();
      }
    };

    return {
      ruleForm: {
        account: "",
        password: "",
        userGroup: "",
      },
      rules: {
        //自定义校验规则
        account: { required: true, validator: validateAcc, trigger: "blur" },
        password: { required: true, validator: validatePass, trigger: "blur" },
        userGroup: {
          required: true,
          message: "请选择分组",
          trigger: "change",
        },
      },
    };
  },

  methods: {
    addAcc() {
      this.$refs["ruleForm"].validate(async (valid) => {
        //valid 表示表单所有字段是否通过验证,只要有一个没有通过,返回false,否则返回true
        if (valid) {
          ///可以发ajax进行登陆操作
          // alert("可以发ajax进行登陆操作");
          let res = await reqAddAcc(this.ruleForm);
          let { code, msg } = res.data;
          if (code == 0) {
            //提示
            // this.$message.success(msg);
            //跳转到列表页
            this.$router.push("/account/accountlist");
          } else {
            alert(msg);
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-input {
  width: 200px;
}
</style>